package com.zjitc.bookshop39.controller;


import com.zjitc.bookshop39.entity.User;
import com.zjitc.bookshop39.service.UserService;
import com.zjitc.bookshop39.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.mysql.cj.conf.PropertyKey.logger;

/**
 * 用户控制器
 * 提供基本的用户管理功能
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody User user){
        Map<String, Object> response = new HashMap<>();
        String jwt = userService.login(user);

        if (jwt != null && !jwt.equals("登录失败")) {
            response.put("success", 200);
            response.put("token", jwt);
            response.put("message", "Success");

            // 设置响应头，同时支持两种方式
            return ResponseEntity.ok()
                    .header("Authorization", "Bearer " + jwt)
                    .header("Jwt", jwt)
                    .body(response);
        } else {
            response.put("success",401);
            response.put("message", "登录失败，用户名或密码错误");
            return ResponseEntity.badRequest().body(response);
        }
    }
//    logout

}
